package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.PageData;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.core.tool.utils.Func;
import com.guodi.map.entity.MapFunctionDir;
import com.guodi.map.entity.MapLayer;
import com.guodi.map.service.IMapCommonService;
import com.guodi.map.service.IMapFunctionAuthService;
import com.guodi.map.service.IMapFunctionDirService;
import com.guodi.map.service.IMapLayerService;
import com.guodi.map.utils.RecursionUtil;
import com.guodi.map.utils.VerificationUtil;
import com.guodi.map.vo.ApiMapFunctionDir;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @描述：地图功能模块目录/图层表控制类
 * @作者: 代鹏刚
 * @日期: 2019-12-13
 */
@Validated
@RestController
@RequestMapping("/MapFunctionDir")
@Api(value = "API - MapFunctionDirController", tags = "地图功能模块目录-图层表")
@Slf4j
public class MapFunctionDirController extends BaseController {

    @Autowired
    private IMapFunctionDirService mapFunctionDirService;
    @Autowired
    private IMapFunctionAuthService mapFunctionAuthService;
    @Autowired
    private IMapCommonService commonService;
    @Autowired
    private IMapLayerService layerService;

    /**
     * @描述: 修改功能参数图层的状态，如果传回的是目录节点id，则会将该目录下所有功能参数状态（status）都改成1；如果传回的是功能参数节点id，则仅修改当前功能参数图层的状态（status）为1。
     *          其余未选中的功能参数图层状态（status）统一置为0
     * @入参: functionCode 功能模块的功能编码，非空必填
     * @入参: selectFunDirIds 选中的id列表，可以是目录的id，也可以是功能参数图层的id，非空必填
     * @入参: isReturnNewFunDir 是否返回新的功能参数列表，根据功能编码和用户角色查询返回，默认不返回
     * @作者: 陈宏成
     * @日期: 2021-03-16
     **/
    @PostMapping("/updateStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionCode", value = "功能模块的功能编码", required = true, dataType = "string", paramType = "query",example = "land-analysis"),
            @ApiImplicitParam(name = "selectFunDirIds", value = "选中的id列表，可以是目录的id，也可以是功能参数图层的id", required = true, dataType = "string", paramType = "query",example = "9ca488310afc4e97ab80672d859cbabf"),
            @ApiImplicitParam(name = "isReturnNewFunDir", value = "是否返回新的功能参数列表，根据功能编码和用户角色查询返回，默认不返回", required = false, dataType = "boolean", paramType = "query")
    })
    @ApiOperation(value = " 修改功能参数图层的状态，选中的功能参数图层status会改成1，也就是默认选中")
    public R updateStatus(String functionCode, String selectFunDirIds, Boolean isReturnNewFunDir){
        if (!StringUtils.isNotBlank(functionCode)) {
            return R.fail(StatusCode.ERROR_CODE_40005,"功能模块的功能编码不能为空");
        }
        List<String> selectFunDirIdsList = new LinkedList<>();
        if(StringUtils.isNotBlank(selectFunDirIds)){
            selectFunDirIdsList = Arrays.asList(selectFunDirIds.split(","));
        }
        // 更新
        this.mapFunctionDirService.updateStatus(functionCode,selectFunDirIdsList,this.getUser());
        // 返回
        if(isReturnNewFunDir != null && isReturnNewFunDir){
            return this.getFunctionDirByRoleId(functionCode,this.getUser().getRoleId());
        }
        return R.data(null);
    }

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2019-12-13
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    public R listByEntity(Page page, MapFunctionDir mapFunctionDir){
        LambdaQueryWrapper<MapFunctionDir> wrapper = new QueryWrapper().lambda();
        IPage<MapFunctionDir> iPage = mapFunctionDirService.page(page, wrapper);
        return R.data(iPage);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MapFunctionDir mapFunctionDir = mapFunctionDirService.getById(id);
        return R.data(mapFunctionDir);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@Valid ApiMapFunctionDir apiMapFunctionDir){
        MapFunctionDir mapFunctionDir = new MapFunctionDir();
        BeanUtils.copyProperties(apiMapFunctionDir,mapFunctionDir);
        if(!prepareData(mapFunctionDir, true)){
            if(mapFunctionDir.getType() == 1){
                return R.fail(StatusCode.ERROR_CODE_40005,"该分类名称已经存在，请更换！");
            }else {
                return R.fail(StatusCode.ERROR_CODE_40005,"该参数名称已经存在，请更换！");
            }
        }
        if(null == mapFunctionDir.getSort()){
            //新增排序
            Integer sort = commonService.getMaxSort(mapFunctionDir.getParentId(), "map_function_dir");
            mapFunctionDir.setSort(sort + 1);
        }
        mapFunctionDir.setId(UuidUtil.get32UUID());
        mapFunctionDirService.save(mapFunctionDir);
        return R.data(mapFunctionDir);
    }

    /**
    * @描述: 新增图层
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/1/9 15:54
    **/
    @PostMapping("/saveLayer")
    @ApiOperation(value = "新增图层")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "图层名称", allowMultiple = true,required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "当前选中目录ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "layerUrl", value = "图层URL", allowMultiple = true,required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "functionId", value = "当前选中功能ID", required = true, dataType = "string", paramType = "query")
    })
    public R saveLayer(String[] name, String parentId, String[] layerUrl, String functionId){
        //验证当前目录下是否存在重名
        List<MapFunctionDir> list = mapFunctionDirService.verifyByName(name,parentId);
        if(list.size() > 0){
            return R.fail(StatusCode.ERROR_CODE_40005,"该目录下已存在"+list.get(0).getName()+"此图层，请更换！");
        }
        Integer sort = commonService.getMaxSort(parentId, "map_function_dir");
        //验证通过后保存
        List<MapFunctionDir> mapFunctionDirList = new ArrayList<>();
        for(int i = 0; i < name.length; i++){
            sort++;
            MapFunctionDir mapFunctionDir = new MapFunctionDir();
            mapFunctionDir.setId(UuidUtil.get32UUID());
            mapFunctionDir.setName(name[i]);
            mapFunctionDir.setParentId(parentId);
            mapFunctionDir.setLayerUrl(layerUrl[i]);
            mapFunctionDir.setFunctionId(functionId);
            mapFunctionDir.setType(2);
            mapFunctionDir.setStatus(1);
            mapFunctionDir.setSort(sort);
            mapFunctionDir.setCreator(getUser().getUserId().toString());
            mapFunctionDir.setCreateTime(new Date());
            //防止误修改
            mapFunctionDir.setModifier(null);
            mapFunctionDir.setModifyTime(null);
            mapFunctionDirList.add(mapFunctionDir);
        }
        mapFunctionDirService.saveBatch(mapFunctionDirList);
        return R.data(mapFunctionDirList);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapFunctionDir mapFunctionDir) {
        if(!prepareData(mapFunctionDir, false)){
            if(mapFunctionDir.getType() == 1){
                return R.fail(StatusCode.ERROR_CODE_40005,"该分类名称已经存在，请更换！");
            }else {
                return R.fail(StatusCode.ERROR_CODE_40005,"该参数名称已经存在，请更换！");
            }
        }
        mapFunctionDirService.updateById(mapFunctionDir);
        return R.data(mapFunctionDir);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2019-12-13
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID", required = true, allowMultiple=true,dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R delete(String[] ids){
        // 删除功能参数授权的数据
        mapFunctionAuthService.deleteByIds(ids);
        // 删除表数据
        mapFunctionDirService.deleteByIds(ids);
        return R.data(null);
    }

    /**
    * @描述: 根据功能id、用户id查询功能授权图层树
    * @作者: 代鹏刚
    * @日期: 2019/12/13 16:59
    **/
    @PostMapping("/listTreeByFunctionIdAndUserId")
    @ApiOperation(value = " 根据专题id、用户id查询专题授权图层树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionId", value = "功能id", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "专题/图层名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isDir", value = "是否为目录层(true:是 false:否)", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "resourceId", value = "资源目录ID", required = true, dataType = "string", paramType = "query")
    })
    public R listTreeByFunctionIdAndUserId() {
        PageData pd = new PageData(this.getRequest());
        List<MapFunctionDir> dataList = mapFunctionDirService.listTreeByFunctionIdAndUserId(pd);
        for(MapFunctionDir dir : dataList){
            if(StringUtils.isNotEmpty(dir.getLayerId())){
                dir.setMapLayer(layerService.getById(dir.getLayerId()));
            }
        }
        //2020-0509兼容pgSQL
        boolean isTrue = dataList.stream().filter(m -> m.getTreeLevel() == 0).findAny().isPresent();
        if(isTrue){
            List<MapFunctionDir> resultList = new ArrayList<>();
            sortList(dataList,"-1",resultList);
            return R.data(resultList);
        }else {
            return R.data(dataList);
        }
    }

    /**
     * @描述: 根据功能id、上级目录ID查询GIS运维-->系统功能-->功能参数，授权图层树实现懒加载
     * @作者: 李贤帆
     * @日期: 2021/05/27 18:25
     **/
    @PostMapping("/getListTreeByFunctionIdAndUserIdLazyLoad")
    @ApiOperation(value = " 根据功能id、上级目录ID查询GIS运维-->系统功能-->功能参数，授权图层树实现懒加载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionId", value = "功能id", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "专题/图层名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isDir", value = "是否为目录层(true:是 false:否)", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "resourceId", value = "资源目录ID", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "上级目录ID", required = true, dataType = "string", paramType = "query")
    })
    public R getListTreeByFunctionIdAndUserIdLazyLoad(@ApiIgnore MapFunctionDir mapFunctionDir) {
        List<MapFunctionDir> dataList = mapFunctionDirService.getListTreeByFunctionIdAndUserId(mapFunctionDir);
        return R.data(dataList);
    }

    public static void sortList(List<MapFunctionDir> list, String id, List<MapFunctionDir> resultList) {
        for (MapFunctionDir node :list) {
            if (node.getParentId().equals(id)) {
                node.setTreeLevel(node.getTreeLevel()+1);
                resultList.add(node);
                sortList(list,node.getId(),resultList);
            }
        }
    }
    /**
    * @描述: 功能模块中关联服务拖拽排序
    * @作者: 代鹏刚
    * @日期: 2020/2/17 11:13
    **/
    @PostMapping("/moveSort")
    @ApiOperation(value = "功能模块中关联服务拖拽排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前拖拽ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "targetId", value = "目标位置ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ahead", value = "是否拖拽到目标位置下方(true:是 false:否)", required = true, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "拖拽到的机构ID", required = true, dataType = "string", paramType = "query")
            })
    public R moveSort(String id , String targetId, Boolean ahead, String orgId){
        //获取目标数据
        MapFunctionDir targetData = mapFunctionDirService.getById(targetId);
        //获取所有跟目标同级的数据
        MapFunctionDir moveData = mapFunctionDirService.getById(id);
        String oldParentId = moveData.getParentId();
        Integer sort = moveData.getSort();
        List<MapFunctionDir> mapFunctionDirs = (List<MapFunctionDir>)mapFunctionDirService.listByMap(VerificationUtil.verifyMap("",moveData.getParentId()));
        if(ahead){
            //拖拽至目标下方的情况
            updateSort(moveData,targetData,orgId,true);
        }else {
            //拖拽至目标上方的情况
            updateSort(moveData,targetData,orgId,false);
        }
        //移动完成后，需要修改被移动数据同级的数据序号
        List<MapFunctionDir> saveData = mapFunctionDirs.stream().filter(s -> !s.getId().equals(id)).collect(Collectors.toList());
        if(saveData.size() > 0){
            if(!saveData.get(0).getParentId().equals(oldParentId)){
                for (MapFunctionDir mapFunctionDir:saveData) {
                    if(mapFunctionDir.getSort() > sort){
                        mapFunctionDir.setSort(mapFunctionDir.getSort() - 1);
                    }
                }
                mapFunctionDirService.updateBatchById(saveData);
            }
        }
        return R.data(null);
    }
    private void updateSort(MapFunctionDir mf, MapFunctionDir targetParent, String orgId, Boolean isAhead){

        //1.查询当前拖拽到的目标父目录下所有的数据
        LambdaQueryWrapper<MapFunctionDir> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MapFunctionDir::getParentId,targetParent.getParentId());
        List<MapFunctionDir> mapFunctionDirList = mapFunctionDirService.list(wrapper);
        if(!isAhead){
            //2.拖拽至目标上方
            //同级拖拽
            if(mf.getParentId().equals(targetParent.getParentId())){
                if(mf.getSort() > targetParent.getSort()){
                    for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                        if(mapFunctionDir1.getSort() < mf.getSort() && mapFunctionDir1.getSort() >= targetParent.getSort()){
                            mapFunctionDir1.setSort(mapFunctionDir1.getSort() + 1);
                        }
                    }
                    //3.更新排序
                    mf.setSort(targetParent.getSort());
                }else {
                    for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                        if(mapFunctionDir1.getSort() > mf.getSort() && mapFunctionDir1.getSort() < targetParent.getSort()){
                            mapFunctionDir1.setSort(mapFunctionDir1.getSort() - 1);
                        }
                    }
                    //3.更新排序
                    mf.setSort(targetParent.getSort() - 1);
                }
            }else {
                for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                    if(mapFunctionDir1.getSort() >= targetParent.getSort()){
                        mapFunctionDir1.setSort(mapFunctionDir1.getSort() + 1);
                    }
                }
                //3.更新排序
                mf.setSort(targetParent.getSort());
            }
            mapFunctionDirService.updateBatchById(mapFunctionDirList);

        }else {
            //2.拖拽至目标下方
            //同级排序
            if(mf.getParentId().equals(targetParent.getParentId())){
                if(mf.getSort() < targetParent.getSort()){
                    for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                        if(mapFunctionDir1.getSort() > mf.getSort() && mapFunctionDir1.getSort() <= targetParent.getSort()){
                            mapFunctionDir1.setSort(mapFunctionDir1.getSort() - 1);
                        }
                    }
                    //3.更新排序
                    mf.setSort(targetParent.getSort());
                }else {
                    for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                        if(mapFunctionDir1.getSort() < mf.getSort() && mapFunctionDir1.getSort() > targetParent.getSort()){
                            mapFunctionDir1.setSort(mapFunctionDir1.getSort() + 1);
                        }
                    }
                    //3.更新排序
                    mf.setSort(targetParent.getSort() + 1);
                }
            }else {
                for (MapFunctionDir mapFunctionDir1:mapFunctionDirList) {
                    if(mapFunctionDir1.getSort() > targetParent.getSort()){
                        mapFunctionDir1.setSort(mapFunctionDir1.getSort() + 1);
                    }
                }
                //3.更新排序
                mf.setSort(targetParent.getSort() + 1);
            }
            mapFunctionDirService.updateBatchById(mapFunctionDirList);
        }
        //4.更新拖拽的数据
        mf.setOrgId(orgId);
//        mf.setParentId(targetParent.getParentId());
        setParentId(mf,targetParent,isAhead);
        mapFunctionDirService.updateById(mf);
    }

    /**
    * @描述: 拖动排序的父节点处理
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/4/28 16:48
    **/
    private void setParentId(MapFunctionDir mf, MapFunctionDir targetParent, Boolean isAhead) {
        /**
         *  目录-->目录
         *    isAhead
         *          true--下方
         *          false
         */
        if (1 == mf.getType() && 1 == targetParent.getType()) {
            if (isAhead) {
                mf.setParentId(targetParent.getId());
            } else {
                String originParentId = mf.getParentId();
                // 平级目录
                mf.setParentId(originParentId);
                // 子节点变平级节点
                if (originParentId.equals(targetParent.getId())) {
                    mf.setParentId(targetParent.getParentId());
                }
            }
        }
        /**
         * 目录-->图层
         */
        if (1 == mf.getType() && 2 == targetParent.getType()) {
            mf.setParentId(targetParent.getParentId());
        }
        /**
         * 图层-->目录
         *      isAhead
         *          true
         *          false
         */
        if (2 == mf.getType() && 1 == targetParent.getType()) {
            //下方
            if (isAhead) {
                mf.setParentId(targetParent.getId());
            } else {
                mf.setParentId(targetParent.getParentId());
            }
        }
        /**
         * 图层-->图层
         */
        if (2 == mf.getType() && 2 == targetParent.getType()) {
            mf.setParentId(targetParent.getParentId());
        }
    }

    /**
    * @描述: 根据用户查询功能服务树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/4/16 14:10
    **/
    @PostMapping("/getFunctionDirByRoleId")
    @ApiOperation(value = "根据用户查询功能服务树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "functionCode", value = "功能服务Code", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "roleIds", value = "当前用户角色ID(多角色用逗号隔开)", required = true, dataType = "string", paramType = "query")
    })
    public R getFunctionDirByRoleId(String functionCode, String roleIds){
        if (StringUtils.isBlank(roleIds)){
            roleIds = AuthUtil.getUser().getRoleId();
        }
        List<MapFunctionDir> mapFunctionDirList = mapFunctionDirService.getFunctionDirByRoleId(functionCode, Func.toStrList(Objects.requireNonNull(roleIds)));
        mapFunctionDirList =getChildLayer(mapFunctionDirList);
        return R.data(RecursionUtil.buildFunctionTree(mapFunctionDirList));
    }

    /**
    * @描述: 补充服务的图层id
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2022/4/28 16:49
    **/
    private List<MapFunctionDir> getChildLayer(List<MapFunctionDir> mapProjectDirs) {
        if (mapProjectDirs == null || mapProjectDirs.isEmpty()) {
            return mapProjectDirs;
        }
        List<MapFunctionDir> result = new ArrayList<>();
        Map<String, List<MapFunctionDir>> serviceList = new HashMap<>();
        Set<String> layerIds = new HashSet<>();
        for (MapFunctionDir mapProjectDir : mapProjectDirs) {
            MapLayer mapLayer = mapProjectDir.getMapLayer();
            if (StringUtils.isBlank(mapLayer.getMid())) {
                mapLayer.setType(null);
            }
            if (mapLayer != null && "-1".equals(mapLayer.getLayerTable())) {
                String mid = mapLayer.getMid();
                List<MapFunctionDir> serviceProjectDirs = serviceList.get(mid);
                if (serviceProjectDirs == null) {
                    serviceProjectDirs = new ArrayList<>();
                }
                layerIds.add(mid);
                serviceProjectDirs.add(mapProjectDir);
                serviceList.put(mid, serviceProjectDirs);
            } else {
                result.add(mapProjectDir);
            }
        }
        Map<String, StringBuilder> childLayerList = new HashMap<>();
        if (!layerIds.isEmpty()) {
            LambdaQueryWrapper<MapLayer> wrapper = Wrappers.lambdaQuery();
            wrapper.in(MapLayer::getParentId, layerIds);
            List<MapLayer> mapLayers = layerService.list(wrapper);
            if (mapLayers != null && !mapLayers.isEmpty()) {
                for (MapLayer mapLayer : mapLayers) {
                    String parentId = mapLayer.getParentId();
                    StringBuilder childLayers = childLayerList.get(parentId);
                    if (childLayers == null) {
                        childLayers = new StringBuilder();
                    }
                    childLayers.append(mapLayer.getLayerTable() + ",");
                    childLayerList.put(parentId, childLayers);
                }
            }
        }
        for (Map.Entry<String, List<MapFunctionDir>> entry : serviceList.entrySet()) {
            List<MapFunctionDir> value = entry.getValue();
            StringBuilder stringBuilder = childLayerList.get(entry.getKey());
            if (stringBuilder != null && stringBuilder.length() > 0) {
                for (MapFunctionDir mapProjectDir : value) {
                    MapLayer mapLayer = mapProjectDir.getMapLayer();
                    mapLayer.setLayerTable(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString());
                }
            }
            result.addAll(value);
        }

        return result;
    }

    private boolean prepareData(MapFunctionDir mapFunctionDir, boolean isSave) {
        //判断同一级目录下，目录名称是否已经存在
        Map map = VerificationUtil.verifyMap(mapFunctionDir.getName(),mapFunctionDir.getParentId());
        map.put("function_id",mapFunctionDir.getFunctionId());
        List<MapFunctionDir> list = (List<MapFunctionDir>)mapFunctionDirService.listByMap(map);
        if(isSave){
            if(list.size() > 0){
                return false;
            }
            mapFunctionDir.setCreator(getUser().getUserId().toString());
            mapFunctionDir.setCreateTime(new Date());
            //防止误修改
            mapFunctionDir.setModifier(null);
            mapFunctionDir.setModifyTime(null);
            return true;
        }else{
            if(list.size() > 0 && !list.get(0).getId().equals(mapFunctionDir.getId())){
                return false;
            }
            mapFunctionDir.setModifier(getUser().getUserId().toString());
            mapFunctionDir.setModifyTime(new Date());
            //防止误修改
            mapFunctionDir.setCreator(null);
            mapFunctionDir.setCreateTime(null);
            return true;
        }
    }
}
